package com.aliwo.algorithm.easy;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 找出某目录下的所有子目录以及子文件并打印到控制台.
 * 使用递归算法
 *
 * @author Xu Yayuan
 * @date 2022/3/26
 */
public class DemoRecursionFileApi {
    public static void main(String[] args) {
        DemoRecursionFileApi demoRecursionFileApi = new DemoRecursionFileApi();
        List<String> paths = new ArrayList<>();
        getAllFilePaths(new File("D:\\SQL必知必会"), paths);
        for (String path : paths){
            System.out.println(path);
        }
    }

    private static void getAllFilePaths(File filePath, List<String> paths) {
        File[] files = filePath.listFiles();
        if (null == files) {
            return;
        }
        for (File f : files) {
            if (f.isDirectory()) { // 判断是否是目录 是目录的话 递归调用
                paths.add(f.getPath());
                getAllFilePaths(f, paths);
            } else {
                paths.add(f.getPath());
            }
        }
    }

}
